﻿using DataAccess;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business
{
    public class OrdersBusiness
    {
        public object GetCount()
        {
            object res = null;
            string query = "select count(1) from orders";
            res = DA.GetOneData(query, CommandType.Text, null, null);
            return res;
        }

        public object GetAmount()
        {
            object res = null;
            string query = "select sum(amount) from orders";
            res = DA.GetOneData(query, CommandType.Text, null, null);
            return res;
        }

        public DataTable GetAllData()
        {
            DataTable dt = null;
            string query = "select * from orders";
            dt = DA.GetDataTable(query, CommandType.Text, null, null);
            return dt;
        }

        public DataTable SearchAllData(OrdersEntity oe)
        {
            DataTable dt = null;
            string query = "select * from orders where username like @username";
            string[] paras = { "@username" };
            object[] values = { "%" + oe.Username + "%" };
            dt = DA.GetDataTable(query, CommandType.Text, paras, values);
            return dt;
        }

        public int AddOrder(OrdersEntity oe)
        {
            int res = 0;
            string query = "insert into orders values (@username,@amount,@orderdate)";
            string[] paras = { "@username", "@amount", "@orderdate" };
            object[] values = { oe.Username, oe.Amount, oe.Orderdate };
            res = DA.ExcuteSql(query, CommandType.Text, paras, values);
            return res;
        }

        public int DeleteOrder(OrdersEntity oe)
        {
            int res = 0;
            string query = "delete from orders where orderid = @orderid";
            string[] paras = { "@orderid" };
            object[] values = { oe.Orderid };
            res = DA.ExcuteSql(query, CommandType.Text, paras, values);
            return res;
        }

        public int DeleteMoreOrders(string sql)
        {
            int res = 0;
            string query = "delete from orders where 1>1 " + sql;
            res = DA.ExcuteSql(query, CommandType.Text, null, null);
            return res;
        }

        public object GetMaxOrderID()
        {
            object res = null;
            string query = "select MAX(orderid) from orders";
            res = DA.GetOneData(query, CommandType.Text, null, null);
            return res;
        }
    }
}
